package com.baofu.paycore.service.processor.preAuth;

import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.dal.mapper.SubPreAuthMapper;
import com.baofu.paycore.dal.model.SubPreAuthDO;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.model.preAuth.SubPreAuthBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.model.CancelPayDTO;
import com.baofu.paycore.service.processor.PayBaseProcessor;
import com.google.common.collect.Lists;
import com.system.commons.sequence.redis.SequenceFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 预授权撤销资金单
 *
 * User: xin deng  Date: 16/8/31 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Component
public class SubPreAuthCancelProcessor implements PayBaseProcessor<PreAuthBO, CancelPayDTO> {

    /**
     * 资金单明细NO
     */
    @Autowired
    private SequenceFacade sequenceFacade;

    /**
     * 预授权资金单
     */
    @Autowired
    private SubPreAuthMapper subPreAuthMapper;

    /**
     * 支付内部对象构建
     *
     * @param payBaseBO  支付模型
     * @param payBaseDTO 请求参数
     */
    @Override
    public PayBaseProcessor process(PreAuthBO payBaseBO, CancelPayDTO payBaseDTO) {
        if(StringUtils.isEmpty(payBaseBO.getOrigPayNo())){
            log.error("原支付单号不存在 预授权信息preAuthBO:{}", payBaseBO);
            throw new PayCoreBizException(PayCoreErrorCode.ORIG_PAY_NO_NOT_EXIST);
        }
        SubPreAuthDO subPreAuthDO = subPreAuthMapper.selectSubPreAuthByPayNo(payBaseDTO.getOrigPayNo(),
                FundTypeEnum.PRE_AUTH.getCode());
        if (null == subPreAuthDO){
            log.error("原支付单号:{} 预授权子单信息不存在", payBaseDTO.getOrigPayNo());
            throw new PayCoreBizException(PayCoreErrorCode.SUB_ORDER_IS_NOT_EXISTED, "预授权子单信息不存在");
        }
        SubPreAuthBO subPreAuthBO = new SubPreAuthBO();
        subPreAuthBO.setFundDetailNo(sequenceFacade.getUniqueSeq());
        subPreAuthBO.setFreezeNo(sequenceFacade.getUniqueSeq());
        subPreAuthBO.setFundType(FundTypeEnum.PRE_AUTH_CANCEL.getCode());
        subPreAuthBO.setPayNo(payBaseBO.getPayNo());
        subPreAuthBO.setCcy(payBaseDTO.getCcy());
        subPreAuthBO.setAmt(payBaseDTO.getAmt());
        subPreAuthBO.setStatus(PayStatusEnums.INIT.getCode());
        subPreAuthConvert(subPreAuthBO, subPreAuthDO);

        payBaseBO.setSubPreAuthBO(Lists.newArrayList(subPreAuthBO));
        return this;
    }

    private void subPreAuthConvert(SubPreAuthBO subPreAuthBO, SubPreAuthDO subPreAuthDO){
        subPreAuthBO.setPayToolType(subPreAuthDO.getPayToolType());
        subPreAuthBO.setPayToolSubType(subPreAuthDO.getPayToolSubType());
        subPreAuthBO.setFundingChannel(subPreAuthDO.getFundingChannel());
        subPreAuthBO.setSubFundingChannel(subPreAuthDO.getSubFundingChannel());
        subPreAuthBO.setPayerAccNo(subPreAuthDO.getPayerAccNo());
        subPreAuthBO.setPayeeAccNo(subPreAuthDO.getPayeeAccNo());
        subPreAuthBO.setRelateFundDetailNo(subPreAuthDO.getRelateFundDetailNo());
        subPreAuthBO.setFreezeCode(subPreAuthDO.getFreezeCode());
    }
}
